<?php
namespace app\common\model;
use think\Model;
use think\Image;
class Upload extends Model{
    public function api($file,$path){
		$config=config('upload');
        if($config['upload_type'] == 1){
            $ftp = new \upload\Ftp();
            $ftp_config = [
                'ftp_host'=>$config['upload_ftp_host'],
                'ftp_port'=>$config['upload_ftp_port'],
                'ftp_user'=>$config['upload_ftp_user'],
                'ftp_pwd' =>$config['upload_ftp_pwd'],
                'ftp_dir'=>$config['upload_ftp_dir'],
            ];
            $ftp->config($ftp_config);
            $res=$ftp->connect();
			if($res['code']>1){
                @unlink($path);				
				return ['msg'=>$res['msg']];
			}
            $ftp->put($path,$file);
			if($config['upload_ftp_unlink']){
			    @unlink($path);
			}
            $file = rtrim($config['upload_http_prefix'],'/').$config['upload_ftp_dir'].ltrim(str_replace(config('upload.upload_path'),'',$file),'/');
        }
        elseif($config['upload_type'] == 2 || $config['upload_graph_control']==1){
            $weibo =  new \upload\weibo();
            $weibo_config = [
                'username'=>$config['upload_sina_username'],
                'password'=>$config['upload_sina_password'],
                'type'=>$config['upload_sina_type'],
            ];			
            $weibo->config($weibo_config);
            $res = $weibo->check();
            if($res['code']>1){
				@unlink($path);
                return ['msg'=>$res['msg']];
            }
            $res = $weibo->upload($path,false);
            if($res['code']>1){
				@unlink($path);
                return ['msg'=>$res['msg']];
            }			
			if($config['upload_ftp_unlink']){
			    @unlink($path);
			}
            $file = $res['url'];
        }
        elseif($config['upload_type'] == 3){
            $bucket = $config['upload_qiniu_bucket'];
            $accessKey = $config['upload_qiniu_accesskey'];
            $secretKey = $config['upload_qiniu_secretkey'];
            $res = self::qiniu($file,$bucket,$accessKey,$secretKey);
            $file = $config['upload_qiniu_http'].'/'. $file;
        }
        elseif ($config['upload_type'] == 4) {
            $ftp = new \upload\Ftp();
            $ftp_config = [
                'ftp_host'=>'v0.ftp.upyun.com',
                'ftp_port'=>21,
                'ftp_user'=>$config['upload_upyun_username'].'/'.$config['upload_upyun_bucket'],
                'ftp_pwd' =>$config['upload_upyun_pwd'],
                'ftp_dir'=>'/',
            ];
            $ftp->config($ftp_config);
            $res=$ftp->connect();
			if($res['code']>1){	
                @unlink($path);			
				return ['msg'=>$res['msg']];
			}
            $ftp->put($path,$file);
			if($config['upload_ftp_unlink']){
			    @unlink($path);
			}
            $file = rtrim($config['upload_http_prefix'],'/').$ftp_config['ftp_dir'].ltrim(str_replace(config('upload.upload_path'),'',$file),'/');
        }
        else{

        }
		$file_url=str_replace(array('http:','https:'),'zanpian:',$file);
        return $file_url;
    }
}
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
?>